package 力扣日常刷题.木22一月.第06天0114;

import 力扣日常刷题.TreeNode;

import java.util.*;

/**
 * @author 帅小伙
 * @date 2022/1/14
 * @description
 */
public class Demo06回溯中等二叉树中和为某一值的路径 {



    ArrayList<List<Integer>> ans = new ArrayList<>();
    Deque<Integer> path = new LinkedList<>();
    public List<List<Integer>> pathSum(TreeNode root, int target) {
        dfs(root, target,path);
        return ans;
    }

    void dfs(TreeNode node, int target,Deque<Integer> path){
        if(node == null) return;
        path.offerLast(node.val);
        target -= node.val;
        if(node.left == null && node.right==null && target == 0){
            ans.add(new LinkedList<>(path));
        }
        dfs(node.left, target,path);
        dfs(node.right, target,path);
        path.pollLast();
    }
}
